package org.boyan.utils;

import com.alibaba.fastjson.JSON;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.boyan.cases.CaseBodyToExcel;
import org.boyan.cases.DemandInfoToExcel;
import org.boyan.cases.NonFunctionalDemandToExcel;
import org.boyan.entity.cases.DemandInfo;
import org.boyan.entity.cases.NonFunctionalDemand;

import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

//将数据导出成excel的方法
public class ExcelExportUtils {

    //将Map数据写入excel
    public static void exportCase(String targetPath, Map<String, Object> bodyMap,boolean editFlag) throws IOException {
        File file = new File(targetPath);
        //拿出需求概要
        Map<String, Object> demandMap = (Map<String, Object>) bodyMap.get("demand");
        DemandInfo demandInfo = JSON.parseObject(JSON.toJSONString(demandMap), DemandInfo.class);
        //将demandInfo中的两个日期相关转换成日期字符串。此处存在部分bug，日期字符串为前端moment格式
        demandInfo.setExpectedDate(demandInfo.getExpectedDate().substring(0,10));
        demandInfo.setExpectedOperationDate(demandInfo.getExpectedOperationDate().substring(0,10));
        DemandInfoToExcel.exportDemandInfo(file, demandInfo);
        //拿出非功能需求
        Map<String, Object> nonFuncMap = (Map<String, Object>) bodyMap.get("nonfunc");
        NonFunctionalDemand nonFunctionalDemand = JSON.parseObject(JSON.toJSONString(nonFuncMap), NonFunctionalDemand.class);
        NonFunctionalDemandToExcel.exportNonFunctionalDemand(file, nonFunctionalDemand);
        //拿出交易的List
        List<Map<String, Object>> bodyList = (List<Map<String, Object>>) bodyMap.get("body");
        CaseBodyToExcel.exportCaseBody(file, bodyList,editFlag);
    }

    //复制一份模板文件到指定位置
    public static void copyModuleFile(String sourcePath, String targetPath) throws IOException {
        File oldFile = new File(sourcePath);
        File newFile = new File(targetPath);
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(newFile);
        byte[] buffer = new byte[1024 * 1024];
        int readByte = 0;
        while ((readByte = in.read(buffer)) != -1) {
            out.write(buffer, 0, readByte);
        }
        in.close();
        out.close();
    }

    //复制行的方法
    public static void insertRow(Sheet sheet, int startRow, int cellNum, int rows) {
        //获取当前行
        Row rowSource = sheet.getRow(startRow);
        //获取当前行样式
        CellStyle rowStyle = rowSource.getRowStyle();
        //向下移动rows行
        sheet.shiftRows(startRow, sheet.getLastRowNum(), rows, true, false);
        for (int i = 0; i < rows; i++) {
            //新增行
            Row rowInsert = sheet.createRow(startRow + i);
            if (rowStyle != null) {
                rowInsert.setRowStyle(rowStyle);
            }
            rowInsert.setHeight(rowSource.getHeight());
            for (int col = 0; col < cellNum; col++) {
                Cell cellSource = rowSource.getCell(col);
                Cell cellInsert = rowInsert.createCell(col);
                if (cellSource != null) {
                    CellStyle cellStyle = cellSource.getCellStyle();
                    //设置单元格样式　　　　
                    if (cellStyle != null) {
                        cellInsert.setCellStyle(cellStyle);
                    }
                }
            }
        }
    }

    //写入excel文件并保存写入信息的方法
    public static void writeWorkbook(Workbook workbook, File file) throws IOException {
        OutputStream out = new FileOutputStream(file);
        workbook.write(out);
        out.flush();
        out.close();
    }

    //复制sheet（格式和内容）查询接口和逻辑，支撑设计等三个sheet
    public static void copySheet(Workbook workbook,int sheetNum,int num){
        for(int i=0;i<num;i++){
            Sheet firstSheet=workbook.cloneSheet(sheetNum);
            Sheet secondSheet=workbook.cloneSheet(sheetNum+1);
            Sheet thirdSheet=workbook.cloneSheet(sheetNum+2);
        }
    }

}
